using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CryptoChat.Models;
using System.IO;

namespace CryptoChat.Controllers
{
    public class ProfileController : Controller
    {
        //
        // GET: /Profile/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Profile/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Profile/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Profile/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
				User user = new User();
				
				user.UserName = User.Identity.Name;
				user.ScreenName = collection["ScreenName"];

				//	Get the avatar upload from the request
				byte[] imageBytes = null;
				if (Request.Files.Count > 0)
				{
					MemoryStream memory = new MemoryStream();
					// Read the file from the input stream to 
					//	a byte array
					byte[] buffer = new byte[4096];
					while (Request.Files[0].InputStream.Read(buffer, 0, buffer.Length) > 0)
					{
						memory.Write(buffer, 0, buffer.Length);
					}
					imageBytes = memory.ToArray();
				}
				user.Avatar = imageBytes;

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Profile/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Profile/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
